package com.story.modular.app.controller;

import com.qiniu.common.QiniuException;
import com.story.core.base.controller.BaseController;
import com.story.core.util.UriUtil;
import com.story.modular.app.service.QiniuService;
import com.story.modular.app.service.UpyunService;
import com.story.modular.app.util.CommonUtil;
import main.java.com.upyun.UpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Administrator
 * @decription 七牛云(又拍云)图片上传服务
 * @Date 2017-12-20 16:08
 */
@Controller
@RequestMapping(value = "/picture")
public class CloudUploadController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger( CloudUploadController.class);

    private static final Long EXPIRE_TIME = 3600L;

    @Autowired
    private QiniuService qiniuService;

    @Autowired
    private UpyunService upyunService;

    /**
     * 七牛云图片上传
     * @param picture
     * @return
     * @throws QiniuException
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public String upload(@RequestPart("file") MultipartFile picture) throws IOException, UpException {
        String filenameExtension = picture.getOriginalFilename().substring(picture.getOriginalFilename().lastIndexOf("."), picture.getOriginalFilename().length());
        InputStream content = picture.getInputStream();
        //String pictureAddr = qiniuService.upload(filenameExtension,content);
        String pictureAddr = upyunService.uploadFile(content,filenameExtension);
        pictureAddr = pictureAddr +"?_upt="+upyunService.getTokenSign(UriUtil.getUri(pictureAddr),EXPIRE_TIME);
        return pictureAddr;
    }
}
